package com.credit.management.system.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.credit.management.system.model.CompanyGuarante;
import com.credit.management.system.model.Page;
import com.credit.management.system.service.ICompanyGuaranteService;
import com.credit.management.system.service.ICompanyService;

@Controller
@RequestMapping("/companyguarante")
public class CompanyGuaranteController {
	@Autowired
	private ICompanyGuaranteService companyGuaranteService;
	@Autowired
	private ICompanyService companyService;
	
	/**
	 * 进入list页面
	 * CompanyGuarante
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(Integer companyid,Model model){
		model.addAttribute("companyid", companyid);
		System.out.println(companyid);
		return "company/companyguarante";	
	}
	@RequestMapping(value="/list2",method=RequestMethod.GET)
	public String list2(Integer companyid,Model model){
		model.addAttribute("companyid", companyid);
		System.out.println(companyid);
		return "company/companyguarante2";	
	}
	
	/**
	 * 分页
	 * @param start
	 * @param length
	 * @param total
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findlist(Integer start, Integer length, Object total,Integer companyid)
			throws UnsupportedEncodingException {
		if (start == null) {
			start = 0;
		};
		if (length == null) {
			length = 5;
		};
		System.out.println("companyid:"+companyid);
		// 查总条数（单表查询），先去联系怎删改
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("companyid", companyid);
		Page page=new Page(start,length);
		page.setParams(params);
		page=companyGuaranteService.findPageCompanyguarante(page); 
		
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aaData", page.getList());//返回的数据
		map.put("iTotalRecords", page.getTotal());//总条数
		map.put("iTotalDisplayRecords",page.getTotal());////总条数
		return map;
	}
	
	
	/**
	 * 添加
	 * @param record
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Integer add(CompanyGuarante record){

		int num=companyGuaranteService.insertSelective(record);
		
		return num;
	}
	/**
	 * 修改
	 * @param record
	 * @return
	 */
	@RequestMapping(value = "/updatecompanyGuarante")
	@ResponseBody
	public Integer updateCompanyGuarante(CompanyGuarante record) {
		
		Map<String,Object> map =new HashMap<>();
		
		Integer num=companyGuaranteService.updateByPrimaryKeySelective(record);
		System.out.println(num);
		return num;
	}
	
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findbyid")
	@ResponseBody
	public CompanyGuarante findbyid(Integer id){
		
		CompanyGuarante companyGuarante=companyGuaranteService.selectByPrimaryKey(id);
		
		return companyGuarante;
	}
	
	@RequestMapping(value="/deletebyid",method=RequestMethod.POST)
	@ResponseBody
	public Integer delete(Integer id){
		Integer result=0;
		
		result=companyGuaranteService.deleteByPrimaryKey(id);
		return result;
	}
	/**
	 * 点击添加，响应公司数据给弹框
	 * @return
	 */
	@RequestMapping(value="/addPre",method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String,Object>> addPre(){
		List<Map<String,Object>> maps=companyService.findAllCompanys();
		return maps;
	}

}
